﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Microsoft.Xna.Framework;

namespace Skyhook.Core.Helpers
{
    static class XmlUtils
    {
        const float Sqrt3 = 1.73205f;

        static public T GetAttributeValue<T>(XmlNode node, string attributename) where T : IConvertible
        {
            if (node == null)
            {
                throw new Exception("Cannot get attribute \"" + attributename + "\" from null node");
            }

            string xpath = "@" + attributename;
            XmlNode resultnode = null;

            try
            {
                resultnode = node.SelectSingleNode(xpath);
            }
            catch (Exception ex)
            {
                throw new Exception("Incorrect XPath \"" + xpath + "\" for node \"" + node.OuterXml + "\"", ex);
            }

            T result = default(T);

            if (resultnode != null)
            {
                try
                {
                    result = (T)Convert.ChangeType(resultnode.Value, typeof(T));
                }
                catch(FormatException ex)
                {
                    throw new Exception("Incorrect format of attribute \"" + attributename + "\" in node \"" + node.OuterXml + "\"",ex);
                }
                return result;
            }

            return default(T);
        }

        static public T[] GetAttributeSeparatedValues<T>(XmlNode node, string attributename,params char[] separator) where T : IConvertible
        {
            if (node == null)
            {
                throw new Exception("Cannot get attribute \"" + attributename + "\" from null node");
            }

            string xpath = "@" + attributename;
            XmlNode resultnode = null;

            try
            {
                resultnode = node.SelectSingleNode(xpath);
            }
            catch (Exception ex)
            {
                throw new Exception("Incorrect XPath \"" + xpath + "\" for node \"" + node.OuterXml + "\"", ex);
            }

            string[] attrstrings = null;
            T[] result = default(T[]);

            if (resultnode != null)
            {
                attrstrings = resultnode.Value.Split(separator);
                result = new T[attrstrings.Length];
                for (int i = 0; i < attrstrings.Length; i++)
                {
                    try
                    {
                        result[i] = (T)Convert.ChangeType(attrstrings[i], typeof(T));
                    }
                    catch (FormatException ex)
                    {
                        throw new Exception("Incorrect format of attribute \"" + attributename + "\" in node \"" + node.OuterXml + "\"", ex);
                    }
                }
                return result;
            }

            return default(T[]);
        }
    }
}
